package org.item.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.item.common.bean.SysRole;
import org.item.common.bean.SysUser;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<SysUser> {

        @Delete("delete from sys_user_role where user_id=#{userid}")
        int deleteUserRoleByUserId(@Param("userid")Integer userid);

        @Insert("insert into sys_user_role value (#{userid},#{roleid})")
        int insertUserRole(@Param("userid")Integer userid,@Param("roleid")Integer roleid);


        @Select("SELECT u.id, u.username, u.password, u.email, u.phone, u.is_super_administrator, u.status, r.role_name " +
                "FROM sys_user u " +
                "LEFT JOIN sys_user_role ur ON u.id = ur.user_id " +
                "LEFT JOIN sys_role r ON ur.role_id = r.id " +
                "WHERE u.username = #{username}")
        SysUser getUserByname(@Param("username") String username);

        @Select("select * from sys_role where id in (select role_id from sys_user_role where user_id = #{userid})")
        List<SysRole> getUserRoles(@Param("userid") Integer userid);

        @Select("select * from sys_user where username = #{username}")
        SysUser getOneByUsername(@Param("username") String username);

}


